package game.views;

import hx.utils.SoundManager;
import hx.ui.UIManager;
import hx.events.MouseEvent;
import hx.events.Event;
import hx.geom.Point;
import hx.display.HBox;
import game.data.XlsData;
import hx.display.Box;

/**
 * 角色选择器功能组件
 */
@:build(hx.macro.UIBuilder.build("assets/views/RoleSelectBar.xml"))
class RoleSelectBar extends Box {
	/**
	 * 头像选项列表
	 */
	public var heads:Array<RoleHeadImage> = [];

	/**
	 * 当前选择的角色
	 */
	public var currentSelectedItem(get, never):RoleHeadImage;

	private function get_currentSelectedItem():RoleHeadImage {
		return heads[selectedIndex];
	}

	override function onInit() {
		super.onInit();
		// 初始化所有角色图标选项
		for (object in this.vbox.children) {
			var hbox:HBox = cast object;
			for (head in hbox.children) {
				heads.push(cast head);
			}
		}
		this.selected_ok.visible = false;
		this.selected.mouseEnabled = false;
		vbox.addEventListener(MouseEvent.CLICK, onRoleClick);
	}

	private function onRoleClick(e:MouseEvent):Void {
		var head:RoleHeadImage = cast e.target;
		if (head != null) {
			var index = heads.indexOf(head);
			this.setSelectIndex(index);
			SoundManager.getInstance().playEffect("move_select");
			this.dispatchEvent(new Event(Event.CHANGE));
		}
	}

	private var __filp:Bool = false;

	private var __roles:Array<Dynamic> = null;

	/**
	 * 设置角色选择数据
	 * @param roles 
	 */
	public function setRolesData(roles:Array<Dynamic>, flip:Bool = false):Void {
		__filp = flip;
		__roles = roles;
		if (flip) {
			// 需要重新排序一下heads循序
			var index = 0;
			for (object in this.vbox.children) {
				var hbox:HBox = cast object;
				for (i => head in hbox.children) {
					heads[getHeadIndexByInt(index)] = cast head;
					index++;
				}
			}
		}
		for (i in 0...heads.length) {
			var role = XlsData.roles.ofType(roles[i]);
			heads[i].setData(role);
		}
	}

	/**
	 * 根据索引获得角色头像的索引位置，因为存在翻转关系，从这里获得的索引才能对应头像
	 * @param index 
	 * @return Int
	 */
	private function getHeadIndexByInt(i:Int):Int {
		if (i == -1) {
			return -1;
		}
		if (__filp) {
			var index = Std.int(i / 7) + 1;
			return index * 7 - (i % 7) - 1;
		}
		return i;
	}

	/**
	 * 角色索引处理
	 * @param page 
	 */
	public function indexTurn(index:Int):Void {
		if (__isLock)
			return;
		selectedIndex += index;
		if (selectedIndex < 0) {
			selectedIndex -= index;
			return;
		} else if (selectedIndex >= __roles.length) {
			selectedIndex = __roles.length - 1;
		}
		SoundManager.getInstance().playEffect("move_select");
		this.setSelectIndex(selectedIndex);
	}

	/**
	 * 当前选择的索引
	 */
	public var selectedIndex:Int = 0;

	/**
	 * 设置当前选择的角色
	 * @param index 
	 */
	public function setSelectIndex(index:Int):Void {
		if (__isLock)
			return;
		selectedIndex = index;
		var head = heads[index];
		if (head == null) {
			this.selected.visible = false;
		} else {
			this.selected.visible = true;
			var pos = new Point(head.x, head.y);
			pos = head.parent.localToGlobal(pos);
			pos = this.globalToLocal(pos);
			this.selected.x = pos.x - 2;
			this.selected.y = pos.y - 2;
			this.selected_ok.x = this.selected.x + this.selected.width / 2 - this.selected_ok.width / 2;
			this.selected_ok.y = this.selected.y + this.selected.height * 0.8 - this.selected_ok.height;
		}
		this.dispatchEvent(new Event(Event.CHANGE));
	}

	private var __isLock:Bool = false;

	/**
	 * 是否已经全部锁定
	 */
	public var isLock(get, never):Bool;

	private function get_isLock():Bool {
		return __isLock;
	}

	/**
	 * 锁定角色选择
	 */
	public function lock():Void {
		__isLock = true;
		this.selected_ok.visible = true;
		SoundManager.getInstance().playEffect("selected");
	}

	/**
	 * 取消锁定角色选择
	 */
	public function unlock():Void {
		__isLock = false;
		this.selected_ok.visible = false;
		SoundManager.getInstance().playEffect("move_select");
	}
}
